<?php

class users extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/user_model', 'admin');
    }

    public function index($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->admin->filter(true);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->admin->filter(FALSE, $offset);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('users/index', $this->pass);
    }

    public function view($id = FALSE) {
        if ($id) {
            $falid_id = $this->admin->is_found(array('id' => $id), array(), 'row', 'users');
            $data['result'] = $falid_id;
            $this->themes->render('users/view', $data);
        } else {
            redirect($this->agent->referrer());
        }
    }

    function send_email($id = false) {
        if ($id) {
            if (!is_numeric($id))
                $id = get_userid_with_username($id);
            $this->pass['user'] = $user = $this->admin->is_found(array('id' => $id), array(), 'row', 'users');
            if ($this->input->post()) {
                $this->load->library('form_validation');
                $this->form_validation->set_rules('subject', lang('label_subject'), 'trim|required');
                $this->form_validation->set_rules('message', lang('label_message'), 'trim|required');
                if ($this->form_validation->run()) {
                    $to = $user->email;
                    $subject = $this->input->post('subject') . ' - Mtjar.com';
                    $msg = $this->input->post('message');
                    $headers = 'From: ' . get_option('site_email');
                    $this->load->library('email_system');
                    $send = $this->email_system->SendEmail($to, $subject, false, false, false, $msg);
                    if ($send === true) {
                        $this->themes->set_message(array('url' => 'users', 'type' => 'success', 'message' => 'message_send_success'));
                    } else {
                        $this->themes->set_message(array('url' => 'users', 'type' => 'validation_error', 'message' => $send));
                    }
                } else {
                    get_validation_errors();
                }
            }
        }
        $this->themes->render('users/send_email', $this->pass);
    }

    public function report($id = false) {
        if ($id) {
            $this->load->model('admin/manager_reports_model', 'mreport');
            $falid_id = $this->mreport->is_found(array('user_id' => $id), array(), FALSE, 'users', 'message_user_no_report', 'DESC');
            $data['id'] = $id;
            $data['result'] = $this->admin->manager_report($falid_id);
            $this->themes->render('users/report', $data);
        } else {
            redirect($this->agent->referrer());
        }
    }

    function getroles($mode = false, $id = false) {
        $pass['mode'] = $mode;
        if ($id) {
            $admin = $this->admin->is_found(array('id' => $id, 'mode' => $mode), array(), 'row');
            if ($admin) {
                $pass['roles'] = $admin->roles;
                $pass['mode'] = $admin->mode;
            }
        }
        $this->load->view(ADMIN_FOLDER . '/users/getroles', $pass);
    }

    public function all() {
        $id = $this->input->post('id');
        $doaction = $this->input->post('doaction');
        // valid if no item select 
        $this->empty_id_actionall($id);
        $this->empty_id_actionall($doaction, 'message_select_action_from_dropdown');

        switch ($doaction) {
            case 'create_manager': {
                    $data['roles'] = serialize($this->input->post('roles'));
                    $data['mode'] = 'manager';
                    $this->admin->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'users', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'create_admin': {
                    $data['roles'] = serialize($this->input->post('roles'));
                    $data['mode'] = 'admin';
                    $this->admin->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'users', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'create_user': {
                    $data['roles'] = '';
                    $data['mode'] = 'user';
                    $this->admin->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'users', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'delete': {
                    $this->admin->delete(array('id' => $id));
                    $this->themes->set_message(array('url' => 'users', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;
            case 'no_trash': {
                    $data['trash'] = 'NO';
                    $this->admin->save($data, array('id' => $id), TRUE);
                    $this->themes->set_message(array('url' => 'users/trash', 'type' => 'success', 'message' => 'message_edit_success'));
                }
                break;

            default:
                show_404();
                break;
        }
    }

    public function login($id = false) {
        if ($id) {
            $this->user_data = $this->admin->is_found(array('id' => $id), array(), 'users');
            $this->login_done('user', base_url(), 'message_login_by_user_success');
        } else {
            redirect($this->agent->referrer());
        }
    }

    public function csv() {
        header('Content-Type: text/html; charset=UTF-8');

        $newline = "\n";
        $csv = '';
        $data = $this->admin->filter(true, 'id,name,email,mobile');


        $r = 0;
        foreach ($data as $value) {
            $r++;
            $csv.='"' . $value->name . '","' . $value->email . '","' . filter_mobile_number($value->mobile) . '",';
            $csv = rtrim($csv);
            $csv .= $newline;
            if ($r == 2000) {
                sleep(2);
            }
        }

        $this->load->helper('download');
        $filename = 'CSV_' . date('d-m-Y-h-i-s') . '.csv';
        force_download($filename, $csv);
    }

    public function no_trash($id = false) {
        if ($id) {
            $falid_id = $this->admin->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            $data['trash'] = 'NO';
            if ($this->admin->save($data, array('id' => $id, 'trash' => 'YES'), TRUE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete_final($id = false) {
        if ($id) {
            $falid_id = $this->admin->is_found(array('id' => $id, 'trash' => 'YES'), array(), 'row', $this->agent->referrer());
            if ($this->admin->delete(array('id' => $id), FALSE)) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function delete($id = false) {
        if ($id) {
            $falid_id = $this->admin->is_found(array('id' => $id), array(), 'row', 'users');
            if ($this->admin->delete(array('id' => $id))) {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'success', 'message' => 'message_delete_success'));
            } else {
                $ref = $this->agent->referrer();
                $this->themes->set_message(array('url' => $ref, 'type' => 'error', 'message' => 'message_delete_error'));
            }
        } else {
            show_404();
        }
    }

    public function pdf() {

// Begin with regular font
        $this->pdf->AddPage();
        $this->pdf->SetFont('Arial', 'B', 16);
        $this->pdf->Cell(40, 10, 'Hello World!');
//$this->pdf->Write(5,'www.oreilly.com','http://www.oreilly.com');
        echo $this->pdf->Output_pdf();
    }

    public function trash($offset = false) {
        $limit = metohd_get_uri('dblimit', ADMIN_LIMIT);
        $count = $this->admin->filter(TRUE, FALSE, TRUE);
        $this->pass['count'] = $count;
        $this->pass['limit'] = $limit;
        $this->pass['result'] = $this->admin->filter(FALSE, $offset, TRUE);
        $this->pass['pagination'] = $this->admin_pagination('index', $count, $limit);
        $this->themes->render('users/trash', $this->pass);
    }

}

?>
